<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\ImportButton;
use App\Models\Scene;
use App\Models\SceneInfo;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class SceneInfoController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('资源及设备');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {


            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {


            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(SceneInfo::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('title','名称');
                $filter->equal('scene_id','景区景点选择')->select(Scene::where('is_docking',1)->get()->pluck('title', 'id'));
            });
            $grid->disableExport();
            $grid->id('ID')->sortable();
            $grid->scene_id('景区名称')->display(function ($val){
                return Scene::all()->pluck('title','id')[$val];
            });
            $grid->year_flow('年平均客流');
            $grid->day_max_flow('单日峰值');
            $grid->max_flow('最大承载量');
            $grid->hotel('酒店(家)');
            $grid->shop('特产商店(个)');
            $grid->restaurant('餐饮(家)');
            $grid->carpark('停车场');
            $grid->toilet('厕所');
            $grid->vav('旅游车船');
            $grid->gate('闸机');
            $grid->camera('摄像头');
            $grid->amusement('娱乐设施');
            $grid->get_ticket_way('取票方式');
            $grid->wifi('wifi覆盖情况')->editable('select', config('options.scene_wifi'));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(SceneInfo::class, function (Form $form) {

            $form->tab('基本信息', function ($form) {
                $form->select('scene_id','景区景点')->options(Scene::where('is_docking',1)->get()->pluck('title','id'))->rules(function ($form) {
                    // 如果不是编辑状态，则添加字段唯一验证
                    if (!$scene_id = $form->model()->scene_id) {
                        return 'unique:scene_info';
                    }
                },['unique'=>'该景区已添加']);
                $form->select('wifi','wifi覆盖情况')->options(config('options.scene_wifi'));
                $form->text('get_ticket_way','取票方式');

            })->tab('景区客流量',function ($form){
                $form->number('year_flow','年平均客流');
                $form->number('day_max_flow','单日峰值');
                $form->number('max_flow','最大承载量');
            })->tab('商户数量',function ($form){
                $form->number('hotel','酒店(家)');
                $form->number('restaurant','餐饮(家)');
                $form->number('shop','特产商店(个)');
            })->tab('景区资源',function ($form){
                $form->number('carpark','停车场');
                $form->number('toilet','厕所');
                $form->number('vav','旅游车船');
                $form->number('gate','闸机');
                $form->number('camera','摄像头');
                $form->number('amusement','娱乐设施');
            });

        });
    }
}
